package cn.carl.frame.core.config;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 * @show 路由信息
 * @version 0.0.1
 * @author EX-CHENQINGSONG001
 *
 */
public interface Route {
	/**
	 * 返回当前route对象
	 * 
	 * @return Route
	 */
	Route init();

	/**
	 * 销毁所有的信息
	 */
	void destroy();

	/**
	 * 停止销毁
	 * 
	 * @return Route
	 */
	default Route stop() {
		routes.clear();
		patterns.clear();
		return this;
	}

	// 初始化
	static final Logger LOG = Logger.getGlobal();
	Map<Class<?>, Pattern> routes = new ConcurrentHashMap<Class<?>, Pattern>();
	List<Pattern> patterns = new CopyOnWriteArrayList<Pattern>();

	/**
	 * 获取类对应的路径信息
	 * 
	 * @param clazz
	 *            Class 执行的类
	 * @return Pattern
	 */
	default Pattern getPattern(Class<?> clazz) {
		return routes.get(clazz);
	}

	/**
	 * 增加Route信息
	 * 
	 * @param clazz
	 *            Class 执行的类
	 * @param pattern
	 *            Pattern 路径
	 * @return Route
	 */
	default Route addRoute(Class<?> clazz, Pattern pattern) {
		Objects.requireNonNull(clazz);
		Objects.requireNonNull(pattern);
		LOG.info(String.format("%s is add to routes,pattern is %s", clazz, pattern));
		routes.put(clazz, pattern);
		return this;
	}

	/**
	 * 获取所有的路径信息
	 * 
	 * @return List<Pattern>
	 */
	default List<Pattern> getPatterns() {
		for (Class<?> ser : routes.keySet()) {
			patterns.add(routes.get(ser));
		}
		return patterns;
	}

	/**
	 * 获取所有的路由信息
	 * 
	 * @return Map<Class<?>, Pattern>
	 */
	default Map<Class<?>, Pattern> getRoutes() {
		return routes;
	}
}
